<cfset mySelf = "index.cfm?event=" />
<style>
div.options{
	position:fixed;
	right:0px;
	bottom:160px;
	padding:5px;
}
</style>
<cfoutput>
<form action="#myself#saveMap" method="post">

<table style="margin-left:10px;" cellpadding="5" cellspacing="0" width="90%">
	<col width="50%" />
	<col width="50%" />
	<tr>
		<td valign="top">
			<table cellpadding="5" cellspacing="0" width="100%">
				<tr>
					<td align="center" colspan="2">Source</td>
				</tr>
				<tr>
					<td align="right">CF Datasource</td>
					<td>
						<select id="source" name="source" class="dsn">	
						</select>
					</td>
				</tr>
				<tr>
					<td align="right">Username</td>
					<td><input type="text" class="textInput" name="sourceusername" /></td>
				</tr>
				<tr>
					<td align="right">Password</td>
					<td><input type="text" class="textInput" name="sourcepassword" /></td>
				</tr>
				<tr>
					<td align="right">Table Name</td>
					<td>
						<select id="sourcetable" name="sourcetable" class="table">	
						</select>
					</td>
				</tr>
				<tr>
					<td align="right" valign="top">Where Clause</td>
					<td>
						<textarea name="sourcecondition" class="textInput" style="width:95%;" rows="8"></textarea>
					</td>
				</tr>
				<tr>
					<td align="right" valign="top">Custom CF Code</td>
					<td>
						<textarea name="sourcecustomcode" class="textInput" style="width:95%;" rows="8"></textarea>
					</td>
				</tr>
			</table>
		</td>
		<td valign="top">
			<table cellpadding="5" cellspacing="0" width="100%">
				<tr>
					<td align="center" colspan="2">Target</td>
				</tr>
				<tr>
					<td align="right">CF Datasource</td>
					<td>
						<select id="target" name="target" class="dsn">	
						</select>
					</td>
				</tr>				
				<tr>
					<td align="right">Username</td>
					<td><input type="text" class="textInput" name="targetusername" /></td>
				</tr>
				<tr>
					<td align="right">Password</td>
					<td><input type="text" class="textInput" name="targetpassword" /></td>
				</tr>
				<tr>
					<td align="right">Table Name</td>
					<td>
						<select id="targettable" name="targettable" class="table">	
						</select>
					</td>
				</tr>
				<tr>
					<td align="right" valign="top">Where Clause</td>
					<td>
						<textarea name="targetcondition" class="textInput" style="width:95%;" rows="8"></textarea>
					</td>
				</tr>
				<tr>
					<td align="right" valign="top">Custom CF Code</td>
					<td>
						<textarea name="targetcustomcode" class="textInput" style="width:95%;" rows="8"></textarea>
					</td>
				</tr>
				
			</table>
		</td>
	</tr>
</table>
<div class="options">
<input type="submit" name="btnSave" value="Save">
<br />
<input type="submit" name="btnSave" value="Run Map.">
</div>
<div id="msg" style="display:block;color:##cc0000;"></div>
<table id="multCols" width="700">
	<tbody >
		<tr>
			<td colspan="6" align="right"></td>
		</tr>
	</tbody>
	<tr>
		<th>Source Column</th>
		<th>Target Column</th>
		<th>CF Var Name</th>
		<th>Target Column Is PK</th>
		<th>Data Type</th>
	</tr>
	<tbody id="columns">
		<tr>
			<td>
				<select id="sourcecolumn" name="sourcecolumn" link="0" class="sourcecolumn">
				</select>
			</td>
			<td>
				<select id="targetcolumn" name="targetcolumn" link="0" class="targetcolumn">
				</select>
			</td>
			<td><input type="text" link="0" id="cfVariablecolumn" name="cfVariablecolumn"></td>
			<td>
			<select name="targetIsPKcolumn" link="0" id="targetIsPKcolumn">
				<option>false</option>
				<option>true</option>
			</select>
			</td>
			<td>
			<select name="datatypecolumn" link="0" id="datatypecolumn">
				<option></option>
				<cfset arrtypes =  application.databaseService.getCFSQLTypes() />
				<cfloop from="1" to="#arrayLen(arrtypes)#" index="i">
				<option>#arrtypes[i]#</option>
				</cfloop>
			</select>
			
			</td>
			<td><input type="button" id="btn" value="+" class="addValue"></td>
		</tr>
		<tr>
			<td align="right" valign="top">Custom CF Code</td>
			<td colspan="4"><textarea name="customcode" id="customcode" rows="5" style="width:100%;border:1px solid black;"></textarea></td>
			<td>&nbsp;</td>
		</tr>	
	</tbody>
	
</table>
</form>
</cfoutput>
<script type="text/javascript">
	cnt = 1;
	function _update(){
		
		$(function(){
			$("select.targetcolumn").change(function(){
				$("#msg").html("");
				if($(this).val() != ""){
				var link = $(this).attr('link');
				var v = $(this).val();
				if(link == 0){
					link='';					
				}
				v = v.split(',');
				$("#datatypecolumn" + link + " option[@value=" + v[0] + "]").attr('selected','selected');
				var sourceVal = $("#sourcecolumn" + link).val();
				sourceVal = sourceVal.split(',');
				if(v[0] != sourceVal[0]){
					$("#msg").html(
					"WARNING!  Your target column is a different data type then your source column, this may cause an error when processing you map."
					);
				}				
			}
				
			})
		})
		
		
	}
	
$().ready(function(){
    $('#columns input.addValue').click(function() {
    	//.appendTo('#multCols');
    	var clonedRow = $(this).parent().parent().parent().clone()
		$("#sourcecolumn", clonedRow).attr({id:"sourcecolumn" + cnt,name:"sourcecolumn" + cnt,link: cnt});
		$("#targetcolumn", clonedRow).attr({id:"targetcolumn" + cnt,name:"targetcolumn" + cnt, link: cnt});
		$("#cfVariablecolumn", clonedRow).attr({id:"cfVariablecolumn" + cnt,name:"cfVariablecolumn" + cnt, link: cnt});
		$("#targetIsPKcolumn", clonedRow).attr({id:"targetIsPKcolumn" + cnt,name:"targetIsPKcolumn" + cnt, link: cnt});
		$("#customcode", clonedRow).attr({id:"customcode" + cnt,name:"customcode" + cnt, link: cnt});		
		$("#datatypecolumn", clonedRow).attr({id:"datatypecolumn" + cnt,name:"datatypecolumn" + cnt, link: cnt});
		$('#datatypecolumn' + cnt + ' option:first').attr('selected', 'selected');
		$('#btn',clonedRow ).attr({'id':'btn' + cnt,"class":"btn"});
		clonedRow.attr("id","colummap" + cnt)
		$("#multCols").append(clonedRow);

    	$(':input.btn',$('#multCols'))
    	   .val('-')
    	   .one('click', function() 
                {
                    $(this).parent().parent().parent().remove();
                    return false;
                }
                );
         cnt ++;
         _update();
	});
});

	

	
	$(document).ready(   
		function(){
			$.getJSON("ajax.cfm",{action: 'getDataSources'}, 
				function(j){
					var options = '<option></option>';
					for (var i = 0; i < j.length; i++) {
						options += '<option value="' + j[i].dsn + '">' + j[i].dsn + '</option>';
					}
					$("select.dsn").html(options);
					$('select.dsn option:first').attr('selected', 'selected');					
				}
			)
		}
	);

	$(function(){
			$("select.dsn").change(function(){
				if($(this).val() != ""){
					var dsnType = $(this).attr('id');
					$.getJSON("ajax.cfm",{action: 'getTableNames',dsn: $(this).val()}, function(j){
						var options = '<option></option>';
						for (var i = 0; i < j.length; i++) {
							options += '<option value="' + j[i].tableName + '">' + j[i].tableName + '</option>';
						}
						$('select#' + dsnType + 'table').html(options);
						$('select#' + dsnType + 'table').attr( 'dsnType', dsnType )
						$('select#' + dsnType + 'table option:first').attr('selected', 'selected');
					})
				}
			})
		})
		
	$(function(){
			$("select.table").change(function(){
				if($(this).val() != ""){
					var dsnType = $(this).attr('dsnType');
					$.getJSON("ajax.cfm",
						{action: 'getColumnInfo',dsn: $("#" + $(this).attr('dsnType')).val(),tablename:$(this).val()},
						function(j){
							var options = '<option></option><option value="cf_sql_varchar,$${cfvar},100">ColdFusion Var</option>';
							for (var i = 0; i < j.length; i++) {
								options += '<option value="' + j[i].columnType + ',' + j[i].columnName + ',' + j[i].columnLen + '">' + j[i].columnName + '</option>';
							}
							$('select.' + dsnType + 'column').html(options);
							$('select.' + dsnType + 'column option:first').attr('selected', 'selected');
							_update();
						}
					)
				}
			})
		})
	
	
		
</script>
